import * as StaticDict from '@/staticDict';
import { SysCustomWidgetType } from '@/staticDict/index.js';

import {
	SysCustomWidgetOperationType
} from '@/staticDict/index.js'
import {
	SysOnlineDictType
} from '@/staticDict/onlineStaticDict.js'

import request from '@/common/request'
import { OnlineOperation } from '@/api/onlineController.js';
import ajax from '@/common/ajax.js';

function getTableDictData(sender,dictId, dictParams) {
	return new Promise((resolve, reject) => {
		let filterDtoList = dictParams ? Object.keys(dictParams).map(key => {
			return {
				columnName: key,
				columnValue: dictParams[key]
			}
		}) : [];
		let params = {
			dictId: dictId,
			filterDtoList: filterDtoList
		}
		OnlineOperation.listDict(sender, params).then(res => {
			resolve(res);
		}).catch(e => {
			reject(e);
		});
	});
}

function getDictDataByUrl(url, params, dictInfo, methods = 'get') {
	return new Promise((resolve, reject) => {
		ajax.doUrl(url, methods, params).then(res => {
			if (Array.isArray(res)) {
				resolve(res.map(item => {
					return {
						id: item[dictInfo.keyColumnName],
						name: item[dictInfo.valueColumnName],
						parentId: item[dictInfo.parentKeyColumnName]
					}
				}));
			} else {
				reject();
			}
		}).catch(e => {
			reject(e);
		});
	});
}

function getUrlDictData(dictInfo, dictParams) {
	let url = dictInfo.dictListUrl;
	if (url != null && url !== '') {
		return getDictDataByUrl(url, dictParams, dictInfo, 'get');
	} else {
		console.error('字典 [' + dictInfo.dictName + '] url为空');
		return Promise.reject();
	}
}

/**
 * 获取字典数据
 * @param {*} dictInfo 字典配置对象
 * @param {*} params 获取字典时传入的参数，仅对于数据表字典和URL字典有效
 * @returns 字典数据
 */
function getDictDataList(sender,dictInfo, dictParams) {
	if (dictInfo == null || dictInfo.dictDataJson == null) return Promise.reject();
	let dictData = JSON.parse(dictInfo.dictDataJson);
	switch (dictInfo.dictType) {
		case SysOnlineDictType.TABLE:
		case SysOnlineDictType.CODE:
			return getTableDictData(sender,dictInfo.dictId, dictParams);
		case SysOnlineDictType.URL:
			return getUrlDictData(dictInfo, dictParams || {});
		case SysOnlineDictType.CUSTOM:
			if (dictData != null && Array.isArray(dictData.dictData)) {
				return Promise.resolve(dictData.dictData);
			} else {
				return Promise.reject(new Error('获取自定义字典数据错误！'));
			}
		case SysOnlineDictType.STATIC:
			if (dictData != null && dictData.staticDictName != null && StaticDict[dictData.staticDictName] != null) {
				return Promise.resolve(StaticDict[dictData.staticDictName].getList());
			} else {
				return Promise.reject(new Error('未知的静态字典！'));
			}
		default:
			return Promise.reject(new Error('未知的字典类型！'));
	}
}

function getOperationPermCode(widget, operation) {
	let datasourceVariableName = (widget.datasource || {}).variableName;
	let temp = 'view';
	switch (operation.type) {
		case SysCustomWidgetOperationType.ADD:
		case SysCustomWidgetOperationType.EDIT:
		case SysCustomWidgetOperationType.DELETE:
			temp = 'edit';
			break;
		default:
			temp = 'view';
	}
	return 'online:' + datasourceVariableName + ':' + temp;
}

export function isChart (widgetType) {
  return [
    SysCustomWidgetType.LineChart,
    SysCustomWidgetType.BarChart,
    SysCustomWidgetType.PieChart,
    SysCustomWidgetType.ScatterChart,
    SysCustomWidgetType.PivotTable,
    SysCustomWidgetType.DataViewTable,
    SysCustomWidgetType.Carousel,
    SysCustomWidgetType.RichText,
    SysCustomWidgetType.GaugeChart,
    SysCustomWidgetType.RadarChart,
    SysCustomWidgetType.FunnelChart,
    SysCustomWidgetType.ProgressBar,
    SysCustomWidgetType.ProgressCircle,
    SysCustomWidgetType.DataCard,
    SysCustomWidgetType.DataProgressCard,
    SysCustomWidgetType.CommonList
  ].indexOf(widgetType) !== -1;
}

export {
	getDictDataList,
	getDictDataByUrl,
	getOperationPermCode
}
